//
//  ZCClearCacheCell.m
//  ZCBaiSi
//
//  Created by zc on 16/7/20.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "ZCClearCacheCell.h"
#import <SDImageCache.h>
#import "NSString+ZCExtension.h"
#import <SVProgressHUD.h>
#define ZCCustormCacheFile  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject
@implementation ZCClearCacheCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        //指示器
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [loadingView startAnimating];
        self.accessoryView = loadingView;
        //设置cell默认的文字
        self.textLabel.text = @"清除缓存(正在计算缓存大小...)";
        //顺序
        //禁止点击  如果设置文字之前userInterEnabled = NO 那么文字会自动变成浅灰色
        self.userInteractionEnabled = NO;
        __weak typeof(self) weakSelf = self;
        
        //获取缓存的图片大小
        //在子线程计算缓存大小
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
            unsigned long long size = [cachesPath stringByAppendingPathComponent:@"Custom"].fileSize;
            size += [SDImageCache sharedImageCache].getSize;
            //如果cell已经销毁了，就直接返回
            if (weakSelf == nil) return ;
            NSString *sizeText = nil;
            if (size >= pow(10, 9)) {
                  sizeText = [NSString stringWithFormat:@"%.2fGB",size / pow(10, 9)];
            }else if (size >= pow(10, 6)){
                  sizeText = [NSString stringWithFormat:@"%.2fMB",size / pow(10, 6)];
            }else if(size >= pow(10, 3)){
                sizeText = [NSString stringWithFormat:@"%.2fKB",size / pow(10, 3 )];
            }else{
                sizeText = [NSString stringWithFormat:@"%.1zdB",size];
            }
            //生成文字
            NSString *text = [NSString stringWithFormat:@"清理缓存(%@)",sizeText];
            //回到主线程设置文字
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.textLabel.text = text;
                //清空右边的指示器
                weakSelf.accessoryView = nil;
                //显示右边的箭头
                weakSelf.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                //添加手势监听器
                [weakSelf addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCache)]];
                //恢复点击事件
                weakSelf.userInteractionEnabled = YES;
                
            });
            
        });
        //[SDImageCache sharedImageCache].getSize;
    }
    return self;
}

//清除缓存
- (void)clearCache{
    //弹出指示器
    [SVProgressHUD showWithStatus:@"正在清除缓存..."];
    //删除缓存文件
    //SDWebImage
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        //自定义的缓存
        NSFileManager *mgr = [NSFileManager defaultManager];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [mgr removeItemAtPath:ZCCustormCacheFile error:nil];
            [mgr createDirectoryAtPath:ZCCustormCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
            //所有的缓存都清除完毕
            dispatch_async(dispatch_get_main_queue(), ^{
                //隐藏指示器
                [SVProgressHUD dismiss];
                //设置文字
                self.textLabel.text = @"清除缓存(0B)";
            });
            
        });
    }];
}

//当cell重新显示到屏幕上时，就会调用一次layoutSubViews
- (void)layoutSubviews{
    [super layoutSubviews];
    //cell重新显示的时候，继续加载
    UIActivityIndicatorView *loadView = (UIActivityIndicatorView *)self.accessoryView;
    [loadView startAnimating];
}

@end
